﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.Master" Inherits="System.Web.Mvc.ViewPage<MethodWorx.CMS.UI.Models.EditWorkflowModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <style>
        div.step { padding:1px; }
        div.arrow { padding-left:175px; }
        div.step button { margin-right:50px; }
        .end { margin-left:130px; }
        img.deleteStep { cursor:pointer; }
    </style>

    <script language="javascript">
        $(document).ready(function() {

            var buttons = new Array();
            /*buttons.push({ text: 'Save', hasText: false, id: 'save', icon: '/images/icons/disk.png' });*/
            buttons.push({ text: 'Create Worflow', hasText: false, id: 'createWorkflow', icon: '<%=Url.Content("~/images/icons/chart_organisation_add.png")%>' });
            buttons.push({ text: 'Delete Worflow', hasText: false, id: 'deleteWorkflow', icon: '<%=Url.Content("~/images/icons/chart_organisation_delete.png")%>' });
            buttons.push({ text: 'Workflow Settings', hasText: false, id: 'workflowSettings', icon: '<%=Url.Content("~/images/icons/cog.png")%>' });
            buttons.push({ text: 'Action', type: 'label' });

            <%if(this.ViewData.Model.Workflows != null && this.ViewData.Model.Workflows.Length != 0) { %>
            buttons.push({ text: 'Action', id: "actions", change: function () { selectAction() }, options:
            [
                <%foreach(var workflow in this.ViewData.Model.Workflows){ %>
                    { value: '<%:workflow.Name %>', text: '<%:workflow.Name %>' }<%=workflow != this.ViewData.Model.Workflows.Last() ? "," : ""%>
                <%} %>
            ]
            });
            MethodWorx.CMS.Toolbar.setButtons(buttons, "#toolbar");
            $("#actions").val('<%=this.ViewData.Model.CurrentWorkflowName ?? "" %>');
            <%} %>
            $(".configure").click(function () {
                var componentId = $(this).attr("componentId");
                var componentName = $(this).attr("componentName");
                if(componentId == null)
                {
                    componentId = $(this).next(".components").val()
                    componentName = $(this).next(".components").children("option:selected").text();
                }
                window.parent.parent.openDialog("<%=Url.Content("~/Plugin/Page")%>/" + componentId + "/<%=this.ViewData.Model.Page.Id %>?workflow=" + $("#actions").val() + "&stepId=" + $(this).attr("stepId") + "&parentStepId=" + $(this).attr("parentStepId"), componentName,
                function(action)
                {
                    window.location.href = window.location.href;
                });
            });

       

            $("#createWorkflow").click(function()
            {
                window.parent.parent.openDialog("<%=Url.Content("~/Workflow")%>/Create/<%=this.ViewData.Model.Page.Id %>", "Create Workflow", function()
                {
                    window.location.href = window.location.href;
                });
            });

            $("#workflowSettings").click(function()
            {
                window.parent.parent.openDialog("<%=Url.Content("~/Workflow")%>/Settings/<%=this.ViewData.Model.Page.Id %>?workflow=" + $("#actions").val(), "Workflow Settings", function()
                {
                    window.location.href = window.location.href;
                });
            });

            $(".showHideBranch").click(function()
            {
                $(this).parent("div").children("div").toggle();
                if($(this).children("span").text() == "show")
                    $(this).children("span").text("hide");
                else
                    $(this).children("span").text("show");
            });

            $(".insertArrow").click(function()
            {
                window.parent.parent.openDialog("<%=Url.Content("~/Page")%>/CreateWorkflowStep/<%=this.ViewData.Model.Page.Id %>?workflow=" + $("#actions").val() + "&stepId=" + $(this).attr("stepId"), "Create Workflow Step", function()
                {
                    window.location.href = window.location.href;
                });
            });

            $(".deleteStep").click(function()
            {
                $.getJSON("<%=Url.Content("~/Page")%>/DeleteWorkflowStep/<%=this.ViewData.Model.Page.Id %>?workflow=" + $("#actions").val() + "&stepId=" + $(this).attr("stepId"), function()
                {
                    window.location.href = window.location.href;
                });
            });

            $("#deleteWorkflow").click(function()
            {
                if(confirm("Are you sure you want to delete this workflow?"))
                {
                    $.getJSON("<%=Url.Content("~/Page")%>/DeleteWorkflow/<%=this.ViewData.Model.Page.Id %>?workflow=" + $("#actions").val(), function()
                    {
                        //  refresh the current page
                        window.location.href = window.location.href;
                    });
                }
            });
        });

        function selectAction() {
            window.location.href = "?name=" + $("#actions").val();
        }



    </script>

    <%if (this.Model.CurrentWorkflow != null && this.Model.CurrentWorkflow.Shared)
      {%>
    <div class="formInfo">
        This workflow is shared between pages that use the page template '<%= this.Model.Page.Template.Name%>'
    </div>
    <%
      }%>

    <%if(this.ViewData.Model.Workflows != null && this.ViewData.Model.Workflows.Length > 0) { %>
    <br />
      <div class="arrow">
                    <img src="<%=Url.Content("~/Images/icons/arrow_down.png")%>" style="cursor:pointer" class="insertArrow" title="double click to insert step" stepId=""/></div>
         <%Html.RenderPartial("EditWorkflowStep", new MethodWorx.CMS.UI.Models.EditWorkflowStepModel()
                              {
                                  Components = this.Model.Components,
                                  Steps = new MethodWorx.CMS.Workflow.WorkflowConfigurationStep [] {
                                            this.Model.CurrentWorkflow.StartingStep,
                                    
                                  },
                                  Parent = null
                              }); %>
    <%} else { %>
    <p style="text-align:center">
        No workflows have been created, please <a href="#" id="createWorkflow">click here</a> to create a workflow.
    </p>
    <%} %>

</asp:Content>
